package com.jch.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jch.reggie.common.BaseContext;
import com.jch.reggie.common.R;
import com.jch.reggie.entity.ShoppingCart;
import com.jch.reggie.service.ShoppingCartSercive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartSercive shoppingCartSercive;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId!=null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartSerciveOne = shoppingCartSercive.getOne(queryWrapper);

        if(cartSerciveOne!=null){
            //如果已经存在，数量加一
            Integer number = cartSerciveOne.getNumber();
            cartSerciveOne.setNumber(number+1);
            shoppingCartSercive.updateById(cartSerciveOne);
        }else {
            //如果不存在，添加到购物车，数量是默认1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartSercive.save(shoppingCart);
            cartSerciveOne=shoppingCart;
        }


        return R.success(cartSerciveOne);

    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartSercive.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartSercive.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
   @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
       LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
       queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
       if(shoppingCart.getDishId()!=null){
           //说明此次减少的商品是菜品
           queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
           ShoppingCart cartSerciveOne = shoppingCartSercive.getOne(queryWrapper);
           Integer number = cartSerciveOne.getNumber();
           //判断购物车里该菜品的数量是否大于1
           if(number>1){
               //数量大于1，数量减一
               cartSerciveOne.setNumber(number-1);
               shoppingCartSercive.save(cartSerciveOne);
           }
           //数量等于1，删除此菜品
           shoppingCartSercive.removeById(cartSerciveOne);
       }
       else {
           //说明减少的商品是套餐
           queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
           ShoppingCart cartSerciveOne = shoppingCartSercive.getOne(queryWrapper);
           Integer number = cartSerciveOne.getNumber();
           //判断购物车里该套餐的数量是否大于1
           if(number>1){
               //数量大于1，数量减一
               cartSerciveOne.setNumber(number-1);
               shoppingCartSercive.save(cartSerciveOne);
           }
           //数量等于1，删除此套餐
           shoppingCartSercive.removeById(cartSerciveOne);
       }
       return R.success("数量减少成功");
    }
}
